package cate.game.role.res.farm;

import cate.common.table.d.GDFarm;
import cate.common.table.d.GDFunc;
import cate.common.table.res.farm.row.FarmGroundRow;
import cate.common.table.res.farm.row.FarmRewardRow;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

@NoteClass("农田")
public class FarmGround {
    @NoteField("位置")
    public byte pos;

    @NoteField("下次可以种植的时间")
    public long nextPlantTime;

    @NoteField("土地等级")
    public int groundLevel;

    @NoteField("种子类型")
    public byte seedType;

    @NoteField("种子等级")
    public int seedLevel;

    @NoteField("预设的种子类型")
    public byte preSeedType;

    @NoteField("预设的种子等级")
    public int preSeedLevel;

    public FarmGround() {

    }

    public FarmGround(byte pos, int groundLevel) {
        this.pos = pos;
        this.groundLevel = groundLevel;
    }

    public GameResult<Void> levelUp(Role role) {
        GameResult<Void> r = new GameResult<>();
        if (seedType > 0) {
            return r.fail("当前不可以升级噢");
        }
        FarmGroundRow nextRow = role.getGame().table.farm.ground.getNextLevelRow(groundLevel);
        if (nextRow == null) {
            return r.fail("地块已经满级啦");
        }
        GameResult<MixResItem> consumeRes = new MixRes(nextRow.levelConsumeStr).consume(role);
        if (!consumeRes.ok()) {
            return r.fail(consumeRes);
        }
        groundLevel++;
        nextPlantTime = System.currentTimeMillis();
        return r;
    }

    public GameResult<Void> plantCheck(GameBody game, int seedType, int seedLevel, boolean isVip, boolean prePlant) {
        GameResult<Void> r = new GameResult<>();
        if (!prePlant && inCd()) {
            //判断土地是否在种植cd中
            return r.fail("种植cd中");
        }
        if (!prePlant && this.seedType > 0) {
            return r.fail("请先收获");
        }
        FarmGroundRow groundRow = game.table.farm.ground.getLevelRow(groundLevel);
        if (groundRow == null) {
            return r.fail("不能种啦");
        }
        if (!groundRow.seeds.contains(seedType)) {
            return r.fail("该种子必须在更高级的土地上种植");
        }
        if (GDFarm.VIP_TREES.contains(seedType) && !isVip) {
            return r.fail("您无法种植该种子");
        }
        FarmRewardRow rewardRow = game.table.farm.reward.getRewardRow(seedType, seedLevel, groundLevel);
        if (rewardRow == null) {
            return r.fail("无法种植该种子");
        }
        return r;
    }

    private boolean inCd() {
        return System.currentTimeMillis() < nextPlantTime;
    }

    public void plantDirect(Role role, byte seedType, int seedLevel, boolean isVip) {
        FarmRewardRow rewardRow = role.getGame().table.farm.reward.getRewardRow(seedType, seedLevel, groundLevel);
        this.seedType = seedType;
        this.seedLevel = seedLevel;
        double actScale = role.getActivity().getEnableActivitySingle(GDFunc.HAPPY_FARM_TASK) == null ? 1 : GDFarm.ACTIVITY_CD_SCALE;
        this.nextPlantTime = System.currentTimeMillis() + (long)((isVip ? rewardRow.vipCdSeconds * 1000 : rewardRow.generalCdSeconds * 1000) * actScale);
        this.preSeedType = 0;
        this.preSeedLevel = 0;
        role.getRes().farm.addSeedTimes(seedType);
    }

    public GameResult<Void> plant(Role role, byte seedType, int seedLevel, boolean isVip) {
        GameResult<Void> r = plantCheck(role.getGame(), seedType, seedLevel, isVip, false);
        if (!r.ok()) {
            return r;
        }
        plantDirect(role, seedType, seedLevel, isVip);
        return r;
    }

    public GameResult<Void> prePlant(Role role, byte seedType, int seedLevel, boolean isVip) {
        GameResult<Void> r = plantCheck(role.getGame(), seedType, seedLevel, isVip, true);
        if (!r.ok()) {
            return r;
        }
        prePlantDirect(role, seedType, seedLevel, isVip);
        return r;
    }

    public void prePlantDirect(Role role, byte seedType, int seedLevel, boolean isVip) {
        if (!inCd() && this.seedType == 0) {
            plantDirect(role, seedType, seedLevel, isVip);
            role.getRes().farm.prePlantBeginPlant(pos, seedType);
        } else {
            this.preSeedType = seedType;
            this.preSeedLevel = seedLevel;
        }
    }

    public GameResult<MixRes> gain(GameBody game, int vipType) {
        GameResult<MixRes> r = new GameResult<>();
        FarmRewardRow rewardRow = game.table.farm.reward.getRewardRow(seedType, seedLevel, groundLevel);
        if (rewardRow == null) {
            return r.fail("您的地块中毒了，怎么找不到奖励啊");
        }
        if (seedType == 0) {
            return r.fail("您还未种植呢");
        }
        seedType = 0;
        seedLevel = 0;
        if (vipType == 0) {
            r.data = new MixRes(rewardRow.generalRewardStr);
        } else if (vipType == 1){
            r.data = new MixRes(rewardRow.vipRewardStr);
        } else if (vipType == 2) {
            r.data = new MixRes(rewardRow.foreverVipRewardStr);
        }
        return r;
    }

    public GameResult<Void> clearCd(Role role) {
        GameResult<Void> r = new GameResult<>();
        if (seedType > 0) {
            return r.fail("请先收获");
        }
        if (System.currentTimeMillis() >= nextPlantTime) {
            return r.fail("无需清除cd");
        }
        boolean overstep = (nextPlantTime - System.currentTimeMillis()) % 60000 > 0;
        long remainMinutes = (nextPlantTime - System.currentTimeMillis()) / 60000 + (overstep ? 1 : 0);
        if (remainMinutes < 1) {
            return r.fail("无需清除cd");
        }
        GameResult<MixResItem> consumeRes = new MixRes(GDFarm.CD_MINUTE_CONSUME).multiply(remainMinutes).consume(role);
        if (!consumeRes.ok()) {
            return r.fail(consumeRes);
        }
        nextPlantTime = System.currentTimeMillis();
        return r;
    }

    public boolean prePlantCheck(Role role, boolean isVip) {
        if (!inCd() && seedType == 0 && preSeedType > 0) {
            byte tmpSeedType = preSeedType;
            plantDirect(role, preSeedType, preSeedLevel, isVip);
            role.getRes().farm.prePlantBeginPlant(pos, tmpSeedType);
            return true;
        }
        return false;
    }

    public void resetPrePlant() {
        preSeedType = 0;
        preSeedLevel = 0;
    }

    public MixRes calcGroundConsume(GameBody game) {
        MixRes reward = new MixRes();
        for (int i = 1; i < groundLevel; ++i) {
            FarmGroundRow groundRow = game.table.farm.ground.getLevelRow(groundLevel);
            if (groundRow != null && StringUtils.isNotBlank(groundRow.levelConsumeStr)) {
                reward.addList(new MixRes(groundRow.levelConsumeStr));
            }
        }
        return reward;
    }
}
